﻿var speed : float;
var turnSpeed : float;
var breakSpeed : float;

var middleBackThrust : GameObject;
var leftBackThrust : GameObject;
var rightBackThrust : GameObject;
var leftFrontThrust : GameObject;
var rightFrontThrust : GameObject;

var shot : GameObject;
var shotting : GameObject;
var objectPosRot : GameObject;

function Awake(){
	if(!networkView.isMine){
		//We aren't the network owner, disable this script
		//RPC's and OnSerializeNetworkView will STILL get trough!
		enabled=false;	
	}
}

function Start () {
	if(networkView.isMine){
		//Only the owner can move the cube!		
		speed = 6;
		turnSpeed = 3;
		breakSpeed = 2;
	}
}

function Update () {
	//if(networkView.isMine){
	//	MovementControls();
	//}
}

function MovementControls() {
	if(networkView.isMine){
		
		if (Input.GetKey(KeyCode.W)) {
			rigidbody.AddForce(speed*transform.forward);
			Play(middleBackThrust);
		}
		else {
			Stop(middleBackThrust);
		}
		
		if (Input.GetKey(KeyCode.A)) {
			transform.Rotate(0,-turnSpeed,0);	
			Play(rightBackThrust);
			Play(leftFrontThrust);
		}
		else {
			Stop(rightBackThrust);
			Stop(leftFrontThrust);
		}
		
		if (Input.GetKey(KeyCode.D)) {
			transform.Rotate(0,turnSpeed,0);
			Play(leftBackThrust);
			Play(rightFrontThrust);
		}
		else {
			Stop(leftBackThrust);
			Stop(rightFrontThrust);
		}
			
		if (Input.GetKey(KeyCode.S)) {
			rigidbody.AddForce(-breakSpeed*transform.forward);		
			Play(leftFrontThrust);
			Play(rightFrontThrust);
		}
		else {
			Stop(leftFrontThrust);
			Stop(rightFrontThrust);
		}	
		if (Input.GetKeyDown(KeyCode.Space)) {
			Shot();
		}
	}
}

function Shot(){

	if (!shotting) {
		shotting = Instantiate(shot,objectPosRot.transform.position, objectPosRot.transform.rotation);	
	}	
}

function Play( thrust : GameObject) {

	var component : ParticleSystem;
	
	component = thrust.GetComponent(ParticleSystem);
	
	if (component.isPlaying == false){
		component.Play();
	}
}

function Stop( thrust : GameObject) {

	var component : ParticleSystem;
	
	component = thrust.GetComponent(ParticleSystem);
	component.Stop();	
	
}